Skip to content

Conversation

@sensei-hacker
Copy link
Member

@sensei-hacker sensei-hacker commented Jan 11, 2026

User description

Summary

Corrects SPI register address masking in busWriteBuf() to clear the MSB instead of setting it.

Problem

For SPI protocol, the MSB indicates read (1) or write (0) operations. The busWriteBuf() function incorrectly used reg | 0x80 which sets the MSB, when it should use reg & 0x7F to clear it for write operations.

This is inconsistent with the correct implementation in busWrite() at line 318.

Solution

Change line 286 from:

return spiBusWriteBuffer(dev, reg | 0x80, data, length);

To:

return spiBusWriteBuffer(dev, reg & 0x7F, data, length);

Impact

Current hardware: None - all devices using busWriteBuf() are I2C-based (VL53L0X, VL53L1X, MLX90393, TERARANGER_EVO, US42 rangefinders).

Future hardware: Prevents silent failures if SPI devices use multi-byte buffer writes.

This is a defensive fix for code correctness.

Fixes #10674


PR Type

Bug fix


Description

  • Corrects SPI register masking in busWriteBuf() to clear MSB for writes

  • Changes register operation from OR (0x80) to AND (0x7F) masking

  • Aligns implementation with correct busWrite() function behavior

  • Prevents future silent failures in SPI multi-byte buffer writes


Diagram Walkthrough

flowchart LR
  A["busWriteBuf SPI path"] --> B["Register masking operation"]
  B --> C["Previous: reg | 0x80 sets MSB"]
  B --> D["Fixed: reg & 0x7F clears MSB"]
  D --> E["Correct write operation"]
  C --> F["Incorrect read operation"]
Loading

File Walkthrough

Relevant files
Bug fix
bus.c
Fix SPI write register masking operation                                 

src/main/drivers/bus.c

  • Changed SPI register masking from reg | 0x80 to reg & 0x7F in
    busWriteBuf()
  • Corrects MSB handling for write operations in SPI protocol
  • Aligns with correct implementation in busWrite() function
  • Affects only theoretical future SPI devices; current devices are
    I2C-based
+1/-1     

Correct the SPI register address masking in busWriteBuf() to clear the
MSB instead of setting it. For SPI protocol, the MSB indicates read (1)
or write (0) operations. Write operations should use (reg & 0x7F) to
clear the MSB, not (reg | 0x80) which sets it.

This matches the correct implementation in busWrite() at line 318 which
uses (reg & 0x7F) for write operations.

Note: This bug affects theoretical future SPI devices using buffer writes.
Current devices using busWriteBuf() are all I2C-based (VL53L0X, VL53L1X,
MLX90393, TERARANGER_EVO, US42 rangefinders) and are unaffected.

Fixes iNavFlight#10674
@qodo-code-review
Copy link
Contributor

PR Compliance Guide 🔍

All compliance sections have been disabled in the configurations.

@github-actions
Copy link

Branch Targeting Suggestion

You've targeted the master branch with this PR. Please consider if a version branch might be more appropriate:

  • maintenance-9.x - If your change is backward-compatible and won't create compatibility issues between INAV firmware and Configurator 9.x versions. This will allow your PR to be included in the next 9.x release.

  • maintenance-10.x - If your change introduces compatibility requirements between firmware and configurator that would break 9.x compatibility. This is for PRs which will be included in INAV 10.x

If master is the correct target for this change, no action is needed.


This is an automated suggestion to help route contributions to the appropriate branch.

@qodo-code-review
Copy link
Contributor

PR Code Suggestions ✨

No code suggestions found for the PR.

@sensei-hacker sensei-hacker changed the base branch from master to maintenance-9.x January 11, 2026 22:15
@sensei-hacker sensei-hacker added this to the 9.1 milestone Jan 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Please correct error in SPI busWriteBuf function.

1 participant